﻿//ko.extenders.latLonCheck = function (target, max) {
//    //add some sub-observables to our observable
//    target.hasError = ko.observable();
//    target.validationMessage = ko.observable();

//    //define a function to do validation
//    function validate(newValue) {
//        var error = $.isNumeric(newValue) && (newValue <= max) && (newValue >= -max);
//        target.hasError(error ? false : true);
//        target.validationMessage(error ? "" : "Must be a valid number between " + -max + " and " + max);
//    }

//    //initial validation
//    validate(target());

//    //validate whenever the value changes
//    target.subscribe(validate);

//    //return the original observable
//    return target;
//};

//ko.extenders.radiusCheck = function (target, max) {
//    //add some sub-observables to our observable
//    target.hasError = ko.observable();
//    target.validationMessage = ko.observable();

//    //define a function to do validation
//    function validate(newValue) {
//        var error = $.isNumeric(newValue) && (newValue <= max) && (newValue >= 0);
//        target.hasError(error ? false : true);
//        target.validationMessage(error ? "" : "Must be a valid number between " + 0 + " and " + max);
//    }

//    //initial validation
//    validate(target());

//    //validate whenever the value changes
//    target.subscribe(validate);

//    //return the original observable
//    return target;
//};

function Criterion(newData) {
    this.id = newData.id;
    this.Value = ko.observable(newData.Value).extend({ required: true });
    this.SiteAttributeId = newData.SiteAttributeId;
    this.SiteAttributeName = newData.SiteAttributeName;
}

function Attribute(newData) {
    this.id = ko.observable(newData.id);
    this.Name = ko.observable(newData.Name);
}

function WishListsViewModel() {
    var self = this;

    // Data
    self.id = ko.observable(data.id);
    self.latitudeNew = ko.observable(data.Latitude).extend({ required: true, max: 90, min: -90 });
    self.longitudeNew = ko.observable(data.Longitude).extend({ required: true, max: 180, min: -180 });
    self.radiusNew = ko.observable(data.Radius).extend({ required: true, max: 21000, min: 1 });
    self.latitude = ko.observable(data.Latitude).extend({ required: true, max: 90, min: -90 });
    self.longitude = ko.observable(data.Longitude).extend({ required: true, max: 180, min: -180 });
    self.radius = ko.observable(data.Radius).extend({ required: true, max: 21000, min: 1 });
    self.siteId = data.SiteId;
    self.userId = data.UserId;
    self.criterions = ko.observableArray(data.Criterions);
    self.newCriterionAttribute = ko.observable();
    self.newCriterionValue = ko.observable().extend({ required: true });
    self.notAssignedAttributes = ko.observableArray(data.NotAssignedAttributes);
    self.wlHub = $.connection.wishListsHub;

    // Operations
    self.addWishList = function () {
        if (self.latitudeNew.isValid() && self.longitudeNew.isValid() && self.radiusNew.isValid()) {
            self.wlHub.server.addWishList(self.userId, self.siteId, self.longitudeNew(), self.latitudeNew(), self.radiusNew());
        }
    }

    self.wlHub.client.confirmAddWishList = function (wlId) {
        self.id(wlId);
        self.latitude(self.latitudeNew());
        self.longitude(self.longitudeNew());
        self.radius(self.radiusNew());
    }

    self.editWishList = function () {
        if (self.latitude.isValid() && self.longitude.isValid() && self.radius.isValid()) {
            self.wlHub.server.editWishList(self.id(), self.longitude(), self.latitude(), self.radius());
        }
    }

    self.addCriterion = function () {
        if (self.newCriterionValue.isValid()) {
            if (typeof self.newCriterionAttribute().id === 'number') {
                self.wlHub.server.addCriterion(self.userId, self.siteId, self.newCriterionAttribute().id, self.newCriterionValue());
            } else {
                self.wlHub.server.addCriterion(self.userId, self.siteId, self.newCriterionAttribute().id(), self.newCriterionValue());
            }
        }
    }

    self.wlHub.client.confirmAddWishList = function (cId) {
        self.criterions.push(new Criterion({ id: cId, Value: self.newCriterionValue(), SiteAttributeId: self.newCriterionAttribute().id, SiteAttributeName: self.newCriterionAttribute().Name }));
        self.notAssignedAttributes.remove(self.newCriterionAttribute());
        self.newCriterionAttribute("");
        self.newCriterionValue("");
    }

    self.editCriterion = function (crit) {
        if((typeof crit.Value === 'function') && (crit.Value().length > 0)) {
            self.wlHub.server.editCriterion(crit.id, crit.Value());
        } else {
            if(crit.Value.length > 0) {
                self.wlHub.server.editCriterion(crit.id, crit.Value);
            }
        }
    }

    self.removeCriterion = function (crit) {
        if (typeof crit.id.length === 'undefined') {
            if (typeof crit.SiteAttributeName === 'function') {
                self.wlHub.server.deleteCriterion(crit.id).done(function () {
                    self.notAssignedAttributes.push(new Attribute({ id: crit.SiteAttributeId(), Name: crit.SiteAttributeName() }));
                    self.criterions.destroy(crit);
                });
            } else {
                self.wlHub.server.deleteCriterion(crit.id).done(function () {
                    self.notAssignedAttributes.push(new Attribute({ id: crit.SiteAttributeId, Name: crit.SiteAttributeName }));
                    self.criterions.destroy(crit);
                });
            }
        } else {
            self.wlHub.server.deleteCriterion(crit.id()).done(function () {
                self.notAssignedAttributes.push(new Attribute({ id: crit.SiteAttributeId(), Name: crit.SiteAttributeName }));
                self.criterions.destroy(crit);
            });
        }
    }

    //self.fsHub.client.confirmAddFriendSite = function (fsId) {
    //    self.friendSites.push(new FriendSite({ id: fsId, Name: self.newNameText(), Url: self.newUrlText(), SiteId: data.SiteId }));
    //    self.newNameText("");
    //    self.newUrlText("");
    //}

    //self.editFriendSite = function (fs) {
    //    self.fsHub.server.editFriendSite(fs.id, fs.Name, fs.Url);
    //}

    //self.removeFriendSite = function (fs) {
    //    self.fsHub.server.deleteFriendSite(fs.id).done(function () {
    //        self.friendSites.destroy(fs);
    //    });
    //};

};

var wlVM = new WishListsViewModel();
ko.applyBindings(wlVM);

$.connection.hub.start();
